home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
500 MB Nyheder Direkte fra Internet 2
/
500 MB nyheder direkte fra internet CD 2.iso
/
start
/
data
/
text
/
hints.txt
< prev
next >
Wrap
Text File
|
1994-09-21
|
16KB
|
329 lines
DEBUGGING
If you write and run your own program, it probably won't work.
Your first reaction will be to blame the computer. Don't!
The probability is 99.99% that the fault is yours. Your program
contains an error. An error is called a bug. Your next task is to
debug the program, which means get the bugs out.
Bugs are common; top-notch programmers make errors all the
time. If you write a program that works perfectly on the first
run and doesn't need debugging, it's called a gold-star program,
and means you should have tried writing a harder one instead!
It's easy to write a program that's almost completely correct,
but hard to find the little bug that's fouling it up. Most of the
time you spend at the computer will be devoted to debugging.
Debugging can be fun. Hunting for the bug is like going on a
treasure hunt ___ or solving a murder mystery. Pretend you're
Sherlock Holmes. Your mission: to find the bug and squish it!
When you squish it, have fun: yell out, ``Squish!''
How can you tell when a roomful of programmers is happy?
Answer: when you hear continual cries of ``Squish!''
To find a bug, use three techniques:
Inspect the program.
Trace the computer's thinking.
Shorten the program.
Here are the details. . . .
Inspect the program
Take a good, hard look at the program. If you stare hard
enough, maybe you'll see the bug.
Ask the computer to help you. Make the computer print the
entire program. To do that, type:
LIST
Popular typos Usually, the bug will turn out to be just a
typing error, a typo. For example. . . .
Maybe you typed the letter O instead of a zero? Typed zero
instead of the letter O?
Typed I instead of 1? Typed 1 instead of I?
Pressed the SHIFT key when you weren't supposed to? Forgot to
press it?
Typed an extra letter? Omitted a letter?
Typed a line you thought you hadn't? Omitted a line?
Fix your strings You must put quotation marks around each
string, and a dollar sign after each string variable:
Right:A$="JERK"
Wrong:A$=JERK
Wrong:A="JERK"
Too much? Here are two reasons why the computer might print too
much:
1. You forgot the insert the word END into your program.
2. You forgot to type NEW; so the computer is reprinting part of
the previous program.
Trace the computer's thinking
If you've inspected the program thoroughly and still haven't
found the bug, the next step is to trace the computer's thinking.
Pretend you are the computer. Do what your program says. Do you
find yourself printing the same wrong answers the computer
printed? If so, why?
To help your analysis, make the computer print everything it's
thinking while it's running your program. For example, suppose
your program contains lines 10, 20, 30, and 40, and uses the
variables B, C, and X$. Insert these lines into your program:
15 PRINT "I'M AT LINE 15. THE VALUES ARE";B;C;X$
25 PRINT "I'M AT LINE 25. THE VALUES ARE";B;C;X$
35 PRINT "I'M AT LINE 35. THE VALUES ARE";B;C;X$
45 PRINT "I'M AT LINE 45. THE VALUES ARE";B;C;X$
Then type the word RUN. The computer will run the program again;
but lines 15, 25, 35, and 45 make the computer print everything
it's thinking. Check what the computer prints. If the computer
prints what you expect in lines 15 and 25, but prints strange
values in line 35 (or doesn't even get to line 35), you know the
bug occurs before line 35 but after line 25; so the bug must be
in line 30.
If your program contains hundreds of lines, you might not have
the patience to insert lines 15, 25, 35, 45, 55, 65, 75, etc.
Here's a short cut. . . .
Halfway down your program, insert a line that says to print all
the values. Then run your program. If the line you inserted
prints the correct values, you know the bug lies underneath that
line; but if the line prints wrong values (or if the computer
never reaches that line), you know the bug lies above that line.
In either case, you know which half of your program contains the
bug. In that half of the program, insert more lines, until you
finally zero in on the line that contains the bug.
Shorten the program
When all else fails,
shorten the program.
Hunting for a bug in a
program is like hunting for a needle in a haystack: the job is
easier if the haystack is smaller. So make your program shorter:
delete the last half of your program. Then run the shortened
version. That way, you'll find out whether the first half of your
program is working the way it's supposed to. When you've
perfected the first half of your program, tack the second half
back on.
Does your program
contain a statement whose meaning you're not completely sure of?
Check the meaning by reading a book or asking a friend; or write
a tiny experimental program that contains the statement, and see
what happens when you type RUN.
Hint: before you shorten
your program (or write tiny experimental ones), SAVE the original
version, even though it contains a bug. After you've played with
the shorter versions, retrieve the original and fix it.
The easiest way to write
a long, correct program is to write a short program first, debug
it, then add a few more lines, debug them, add a few more lines,
debug them, etc. In other words, start with a small program,
perfect it, and then gradually add perfected extras so you
gradually build a perfected masterpiece. If you try to compose a
long program all at once ___ instead of building it from
perfected pieces ___ you'll have nothing more than a mastermess
___ full of bugs.
Moral: to build a large
masterpiece, start with a small masterpiece. Putting it another
way: to build a skyscraper, begin by laying a good foundation;
double-check the foundation before you start adding the walls and
the roof.
ERROR MESSAGES
If you type a command that the computer can't obey, the
computer will gripe by printing an error message.
You can make 6 kinds of errors: syntax errors, numeric errors,
printer errors, logic errors, disk errors, and advanced errors.
Here are the most popular error messages printed by the IBM PC,
in the order you'll probably encounter them.
Syntax errors
If you say PRIND instead of PRINT, the computer will say SYNTAX
ERROR. That means the computer hasn't the faintest idea of what
you're talking about!
If the computer says you have a SYNTAX ERROR, it's usually
because you spelled a word wrong, or forgot a word, or used a
word the computer doesn't understand. It can also result from
wrong punctuation: check your commas, semicolons, and colons. It
can also mean you typed a left parenthesis that doesn't have a
corresponding right parenthesis, or vice versa.
It can also mean your DATA statement contains a string but your
READ statement says to read a number instead. To fix that
problem, change the READ statement by putting a dollar sign at
the end of the variable's name.
Numeric errors
In a statement such as PRINT 5+2, the + is called the
operation. The 5 and 2 are called the operands.
If you try to say PRINT 5+2 but forget to type the 2, the
computer will say MISSING OPERAND.
The biggest number the computer can handle is about 1E38 (which
means 1 followed by 38 zeros). If you try to go much higher, the
computer will say OVERFLOW. For example, if you say 1E39, or you
try to multiply together lots of big numbers, you'll get an
OVERFLOW ERROR.
If you try to divide by zero, the computer will say DIVISION BY
ZERO.
If you feed the computer a number that's inappropriate, the
computer will say ILLEGAL FUNCTION CALL. For example, if you say
DELETE 40, but your program doesn't contain any line whose number
is 40, the computer will say ILLEGAL FUNCTION CALL.
Printer errors
If your printer runs out of paper, the computer will say OUT OF
PAPER.
When the computer sends a message to a device (such as your
printer), the computer waits for the device to respond to the
message. If the computer has waited a long time without receiving
any response from the device, the computer will give up waiting;
it will say DEVICE TIMEOUT.
When you try using your printer, if the computer says DEVICE
TIMEOUT it means the computer isn't receiving the proper feedback
from your printer. That's because your printer isn't properly
attached to the computer, or hasn't been turned on, or isn't in a
state of being READY.
Logic errors
If you say GO TO 40 but there isn't a line 40 in your program,
the computer will say UNDEFINED LINE NUMBER. The computer says
UNDEFINED LINE NUMBER whenever
you try to GO TO a missing line.
The computer handles two
types of information: numbers and strings. If you feed the
computer the wrong type of information ___ if you feed it a
number when you should have fed it a string, or you feed it a
string when you should have fed it a number ___ the computer will
say TYPE MISMATCH.
When you feed the
computer a string, you must put the string in quotation marks,
and put a dollar sign after the string's variable. If you forget
to type the string's quotation marks or dollar sign, the computer
won't realize it's a string; the computer will think you're
trying to type a number instead; and if a number would be
inappropriate, the computer will say TYPE MISMATCH. So when the
computer says TYPE MISMATCH, it usually means you forgot a
quotation mark or a dollar sign.
Disk errors
To use the disks, you
must invent a name for your program. If the name you invent is
inappropriate, the computer will say BAD FILE NAME.
If you forget to put a
disk into the disk drive, or you accidentally leave the disk
drive's door open, the computer will say DISK NOT READY. If the
disk's surface is scratched or otherwise damaged, the computer
will say DISK MEDIA ERROR.
If the disk is
write-protected by a write-protect tab (which stops you from
writing onto the disk) and you nevertheless try to write
information onto the disk, the computer will say DISK WRITE
PROTECT. If you try to write information onto the disk but the
disk doesn't have enough free space to hold the information, the
computer will say DISK FULL.
The disk's directory is
a place on the disk that remembers the names of all the disk's
programs and files. If the directory is full, so that there's no
more room to store names of programs, and you nevertheless try to
put another program onto the disk, the computer will say TOO MANY
FILES.
If you tell the computer
to copy JOE from the disk to the RAM (by saying LOAD ``JOE'' or
RUN ``JOE''), but the computer can't find JOE on the disk, the
computer will say FILE NOT FOUND.
Advanced errors
If you say READ, but the
computer can't find any more DATA to read (because the computer
has read all the DATA already), the computer will say OUT OF
DATA.
Whenever you say FOR,
you're supposed to say NEXT, and vice versa. If you say FOR
without saying NEXT, the computer will say FOR WITHOUT NEXT. If
you say NEXT without saying FOR, the computer will say NEXT
WITHOUT FOR. If your program contains the words FOR and NEXT
several times, but you accidentally have more FOR's than NEXT's
or more NEXT's than FOR's, the computer will print one of those
error messages, because the number of FOR's ought to equal the
number of NEXT's.
If your computer doesn't
have enough RAM to hold your information, it will say OUT OF
MEMORY. Part of the RAM is used for handling strings; if that
part isn't big enough to hold all your strings, the computer will
say OUT OF STRING SPACE. If you get one of those error messages,
shorten your program (by splitting it into several smaller
programs).
CONVENIENCES
The following commands make your life easier.
Renumbering
Suppose you've written a sloppy program, like this:
37 PRINT "SNARL"
68 PRINT "SMIRK"
91 GO TO 37
If you type ___
RENUM
LIST
the computer will renumber your program, so that the lines are
numbered 10, 20, and 30. The computer will say:
10 PRINT "SNARL"
20 PRINT "SMIRK"
30 GO TO 10
Notice that the computer even changed the ``GO TO 37'', so that
it became ``GO TO 10''!
If you type ___
RENUM 500
LIST
the computer will renumber your program so that it begins at line
500. The computer will say:
500 PRINT "SNARL"
510 PRINT "SMIRK"
520 GO TO 500
Suppose you wrote a program that contained a line numbered 83,
and you'd like that line to be called ``line 500'' instead.
Here's how to start renumbering at line 83, so that line 83
becomes 500, and the next line becomes 600, and the line after
that becomes 700, etc.:
RENUM 500,83,100
LIST
That command makes the computer renumber the program so that 500
is the new number for line 83, and the next lines are numbered
600, 700, 800, etc. (going up by 100's).
The RENUM command works on most computers. To find out about
your computer, read the ``Versions of BASIC'' appendix.
The PAUSE key
Magicians often say, ``The hand is
quicker than the eye.'' The computer's the ultimate magician: the
computer can print information on the screen much faster than you
can read it.
When the computer is printing too
fast for you to read, tap the PAUSE key. The computer will pause,
to let you read what's on the screen.
When you've finished reading what's
on the screen, and want the computer to stop pausing, tap the
PAUSE key again. Then the computer will continue printing
rapidly, where it left off.
If your eyes are as slow as mine,
you'll need to use the PAUSE key often! You'll want the computer
to pause when you're running a program that contains many PRINT
statements, or if you're running a program that contains a PRINT
statement in a loop, or if you're LISTing a long program.
The PAUSE key is on the Walter
keyboard. To find out whether it's on your computer's keyboard,
check the ``Versions of BASIC'' appendix.
Apostrophe
Occasionally, jot a note, to remind
yourself what your program does and what the variables stand for.
Slip the note into your program by putting an apostrophe before
it:
10 'THIS PROGRAM IS ANOTHER DUMB EXAMPLE
20 'WRITTEN BY RUSSY-POO, A STUPID JERK
30 'IT WAS WRITTEN ON HALLOWEEN, UNDER A FULL MOON, SHADED BY HIS
FANGS
40 C=40' BECAUSE RUSS HAS 40 COMPUTERS
50 H=23' BECAUSE 23 OF HIS COMPUTERS ARE HAUNTED
60 PRINT C-H' THAT IS HOW MANY COMPUTERS ARE UNHAUNTED AND SAFE
FOR KIDS
When you type RUN, the computer ignores everything that's to the
right of an apostrophe. So the computer ignores lines 10, 20, and
30; in lines 40 and 50 the computer ignores the ``because . . .
''; in line 60 the computer ignores the comments about being
unhaunted. Since C is 40, and H is 23, line 60 makes the computer
print:
17
Everything to the right of an
apostrophe is called a comment (or remark). While the computer
runs the program, it ignores the comments. But whenever you say
LIST, the computer will LIST the entire program, including even
the comments. Although the comments appear in the LIST, they
don't affect the RUN.